package com.hutao.medical.controller.statics;

import java.io.OutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hutao.medical.common.util.ExcelUtil;
import com.hutao.medical.common.util.Result;
import com.hutao.medical.pojo.dto.MedicalSearchQuery;
import com.hutao.medical.service.AuditRecordDetailService;
import com.hutao.medical.service.AuditStandardService;
import com.hutao.medical.service.BasicService;
import com.hutao.medical.service.MedRecordAuditService;

@Controller
@RequestMapping("/audit")
public class MedRecordAuditController {
	private Logger log = LoggerFactory.getLogger(MedRecordAuditController.class);

	@Autowired
	private MedRecordAuditService medRecordAuditService;
	
	@Autowired
	private BasicService basicService;
	
	@Autowired
	private AuditRecordDetailService auditRecordDetailService;
	
	@Autowired
	private AuditStandardService auditStandardService;
	
	@ResponseBody
	@RequestMapping("/getMedRecordList")
	public Result getMedRecordList(MedicalSearchQuery medicalSearchQuery){
		Result result = null;
		try {
			Map<String,Object> map = new HashMap<>();
			List<Map<String,Object>> list=medRecordAuditService.selectMedicalRecordRetrievaList(medicalSearchQuery);
			
			if (null != list && list.size() > 0) {
				for(Map<String, Object> audetailMap : list) {
					String firstGuid = (String) audetailMap.get("guid");
					long repairCount = auditRecordDetailService.findCountByFristGuid(firstGuid);
					audetailMap.put("repairCount", repairCount);
				}
			}
			
			int totalCount = medRecordAuditService.selectMedicalRecordRetrievaCount(medicalSearchQuery);
			map.put("list", list);
			map.put("totalCount", totalCount);
			result=new Result(true,"查询成功!",map);
		} catch (Exception e) {
			e.printStackTrace();
			result = new Result(false,"查询失败!");
		}
		return result;
	}

	
	public String getDefectId(Map<String, Object> approvalDateBirthMap, String defectFlag) {
		String result = "";
		if (null != approvalDateBirthMap.get(defectFlag)) {
			if(!((String)approvalDateBirthMap.get(defectFlag)).equals("false")) {
				String a = (String)approvalDateBirthMap.get(defectFlag);
				result = a;
			}
		}
		return result;
	}
	
	public String getDefect(Map<String, Object> approvalDateBirthMap, String defectFlag) {
		String result = "";
		if (null != approvalDateBirthMap.get(defectFlag)) {
			if(!((String)approvalDateBirthMap.get(defectFlag)).equals("false")) {
				Integer a = Integer.parseInt((String)approvalDateBirthMap.get(defectFlag));
				Map<String, Object> auditStandardMap = auditStandardService.findAuditStandardById(a);
				
				if (null != auditStandardMap) {
					result = auditStandardMap.get("name")+",";
				}
			}
		}
		return result;
	}
	
	/**
	 * 审核标准
	 * @param name
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/getAuditStandard")
	public Result getAuditStandard(String name){
		Result result = null;
		try {
			List<Map<String,Object>> list=basicService.getAuditStandard(name);
			result=new Result(true,"查询成功!",list);
		} catch (Exception e) {
			e.printStackTrace();
			result=new Result(false,"查询失败!");
		}
		return result;
	}
	

	/*
	 *  跳转到自动审核详情页面
	 */
	@RequestMapping("/Audit_detail")
	public String Audit_detail(){
		return "statics/indexAudit/Audit_detail";
	}

	
	
	
}
